﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Diagnostics;

namespace NetC
{
    public partial class Form_setting : Form
    {
        public Form_setting()
        {
            InitializeComponent();

            LoadForm();
        }

        private void trackBar_limit_ValueChanged(object sender, EventArgs e)
        {
            label_limit.Text = trackBar_limit.Value.ToString() + "MB";
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool saveSetting()
        {
            try
            {

                RegistryKey mysoftware = Registry.CurrentUser.OpenSubKey("software", true);
                RegistryKey netconn = mysoftware.CreateSubKey("NetC");
                if (netconn == null)
                    netconn = mysoftware.CreateSubKey("NetC");

                netconn.SetValue("cserver", textBox_customer_server.Text);
                netconn.SetValue("cport", textBox_custome_port.Text);
                netconn.SetValue("cversion", textBox_cversion.Text);

                netconn.SetValue("wserver", textBox_web_server.Text);
                netconn.SetValue("wversion", textBox_wversion.Text);

                if (radioButton_tryagain.Checked) netconn.SetValue("exception", "0");
                else netconn.SetValue("exception", "1");

                if (checkBox_startRun.Checked) netconn.SetValue("startrun", "1");
                else netconn.SetValue("startrun", "0");
              
                netconn.SetValue("limitTh", trackBar_limit.Value.ToString());
                netconn.SetValue("cmlatency", trackBar_cmlatency.Value.ToString());

                if (checkBox_rm.Checked)
                {
                    HandleReg.set_regValue("moneyrm", "1");
                }
                else
                {
                    HandleReg.set_regValue("moneyrm", "0");
                }
                mysoftware.Close();
                return true;
            }
            catch (System.Exception e)
            {
                 return false;
                throw new Exception(e.ToString());
            }
        }


        private void LoadForm()
        {
            try
            {

                RegistryKey mysoftware = Registry.CurrentUser.OpenSubKey("software", true);
                RegistryKey netconn = mysoftware.CreateSubKey("NetC");
                if (netconn == null)
                {
                    return;
                }
                else
                {
                    //set the client connection parameters
                    textBox_customer_server.Text = netconn.GetValue("cserver").ToString();
                    textBox_custome_port.Text = netconn.GetValue("cport").ToString();
                    textBox_cversion.Text = netconn.GetValue("cversion").ToString();
                    //set the web connection parameters
                    textBox_web_server.Text = netconn.GetValue("wserver").ToString();
                    textBox_wversion.Text = netconn.GetValue("wversion").ToString();

                    radioButton_tryagain.Checked =  (netconn.GetValue("exception").ToString() == "0" ? true : false);
                    radioButton_remind.Checked = (!radioButton_tryagain.Checked);

                    checkBox_startRun.Checked = (netconn.GetValue("startrun").ToString() == "1" ? true : false);
                    checkBox_rm.Checked = (netconn.GetValue("moneyrm").ToString() == "1" ? true : false);

                    int TH = Convert.ToInt32(netconn.GetValue("limitTh"));
                    if (TH >= trackBar_limit.Minimum && TH <= trackBar_limit.Maximum)
                        trackBar_limit.Value = TH;

                    int latency = Convert.ToInt32(netconn.GetValue("cmlatency"));
                    if (latency >= trackBar_cmlatency.Minimum && latency <= trackBar_cmlatency.Maximum)
                        trackBar_cmlatency.Value = latency;

                    mysoftware.Close();
                }
            }
            catch (System.Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        private void startRun(bool IsStart)
        {
            if (IsStart)
            {
                try
                {
                    string strAssName = Application.StartupPath + @"\" + Application.ProductName + @".exe";
                    string ShortFileName = Application.ProductName;

                    RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                    if (rgkRun == null)
                    {
                        rgkRun = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
                        rgkRun.SetValue(ShortFileName, strAssName);
                    }
                    else
                    {
                        rgkRun.SetValue(ShortFileName, strAssName);
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString() + "\n请尝试用管理员身份运行。");
                }
            }
            else
            {
                try
                {
                    Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true).DeleteValue(Application.ProductName, false);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString() + "\n请尝试用管理员身份运行。");
                }
            }

        }

        private void button_ok_Click(object sender, EventArgs e)
        {
            if (saveSetting())
            {
                this.Hide();
                MessageBox.Show("配置成功，已经应用生效。","NetC",MessageBoxButtons.OK,MessageBoxIcon.Information);
                this.Close();

            }
            else
            {
                MessageBox.Show("配置不成功，请检查确认。", "NetC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            startRun(checkBox_startRun.Checked);

        }

        private void linkLabel_creg_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
                HandleReg.resetReg();
                MessageBox.Show("注册表清理完成。", "NetC", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void linkLabel_srunLogin_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string http_add = "http://10.0.0.55:8800/services1.php?checkid=9538&user_login_name=" + HandleReg.getRegValue("username") + "&user_password=" + HandleReg.passencode_in(HandleReg.getRegValue("password"));
            Process myprocess = new Process();
            string IEFileName = Environment.SystemDirectory;
            IEFileName = IEFileName.Substring(0, 2);
            IEFileName = IEFileName + @"\Program Files\Internet Explorer\iexplore.exe";

            myprocess.StartInfo.FileName = IEFileName;
            myprocess.StartInfo.Arguments = http_add;
            myprocess.Start();
        }

        private void linkLabel_srunNotice_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string http_add = "http://" + HandleReg.getRegValue("wserver");
            Process myprocess = new Process();
            myprocess.StartInfo.FileName = "iexplore.exe";
            myprocess.StartInfo.Arguments = http_add;
            myprocess.Start();
        }

        private void trackBar_cmlatency_ValueChanged(object sender, EventArgs e)
        {
            label_latency.Text = trackBar_cmlatency.Value.ToString() + "S";
        }

    }
}
